//
//  JGEmojiCell.m
//  JGEmojiLabelDemo
//
//  Created by mac on 15/12/1.
//  Copyright © 2015年 Free. All rights reserved.
//

#import "JGEmojiCell.h"
#import "MLEmojiLabel.h"

#define kWidth 300.0f

@interface JGEmojiCell () <MLEmojiLabelDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, strong) MLEmojiLabel *emojiLabel;

@end

@implementation JGEmojiCell

-(MLEmojiLabel *)emojiLabel
{
    if (!_emojiLabel) {
        self.emojiLabel = [MLEmojiLabel new];
        self.emojiLabel.numberOfLines = 0;
        self.emojiLabel.font = [UIFont systemFontOfSize:14.0f];
        self.emojiLabel.delegate = self;
        self.emojiLabel.backgroundColor = [UIColor clearColor];
        self.emojiLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        self.emojiLabel.textColor = [UIColor whiteColor];
        self.emojiLabel.backgroundColor = [UIColor colorWithRed:0.218 green:0.809 blue:0.304 alpha:1.000];
        self.emojiLabel.textInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        
        self.emojiLabel.isNeedAtAndPoundSign = YES;
        self.emojiLabel.disableEmoji = NO;
        
        self.emojiLabel.lineSpacing = 3.0f;
        
        self.emojiLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
    }
    
    return _emojiLabel;
}

- (void)awakeFromNib
{
    // Initialization code
    [self.contentView addSubview:self.emojiLabel];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self.contentView addSubview:self.emojiLabel];
        
#warning 这里是在父View添加手势后，不影响emojiLabel的链接点击的范例，参考下面的gestureRecognizer:shouldReceiveTouch:方法内实现
        UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        tapG.delegate = self;
        [self.contentView addGestureRecognizer:tapG];
    }
    return self;
}

- (void)tap
{
    NSLog(@"tapped");
}

#pragma mark - gesture delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    //如果是在label的链接中，添加进去的点击手势不执行
    return ![self.emojiLabel containslinkAtPoint:[touch locationInView:self.emojiLabel]];
}

-(void)setEmojiText:(NSString *)emojiText
{
    _emojiText = emojiText;
    
    [self.emojiLabel setText:emojiText];
}

#pragma mark - 计算cell 的高度
+(CGFloat)heightForEmojiText:(NSString *)emojiText
{
    static MLEmojiLabel *protypeLabel = nil;
    if (!protypeLabel) {
        protypeLabel = [MLEmojiLabel new];
        protypeLabel.numberOfLines = 0;
        protypeLabel.font = [UIFont systemFontOfSize:14];
        protypeLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        protypeLabel.textInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        protypeLabel.isNeedAtAndPoundSign = YES;
        protypeLabel.disableEmoji = NO;
        protypeLabel.lineSpacing = 3.0f;
        
        protypeLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
    }
    
    [protypeLabel setText:emojiText];
    
    return [protypeLabel preferredSizeWithMaxWidth:kWidth].height;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.emojiLabel.frame = CGRectMake(10.0f, 5.0f, kWidth, self.contentView.frame.size.height-5.0f*2);
}


#pragma mark - MLEmojiLabelDelegate
-(void)mlEmojiLabel:(MLEmojiLabel *)emojiLabel didSelectLink:(NSString *)link withType:(MLEmojiLabelLinkType)type
{
    switch (type) {
        case MLEmojiLabelLinkTypeURL:
            NSLog(@"点击了链接%@",link);
            break;
        case MLEmojiLabelLinkTypePhoneNumber:
            NSLog(@"点击了电话%@",link);
            break;
        case MLEmojiLabelLinkTypeEmail:
            NSLog(@"点击了邮箱%@",link);
            break;
        case MLEmojiLabelLinkTypeAt:
            NSLog(@"点击了用户%@",link);
            break;
        case MLEmojiLabelLinkTypePoundSign:
            NSLog(@"点击了话题%@",link);
            break;
        default:
            NSLog(@"点击了不知道啥%@",link);
            break;
    }
    
}


@end
